<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // 统计全部包含2的数字出现次数
        var sum = 0;
        for(var i = 1;i <= 100;i++) {
            var ge = parseInt(i % 10);
            var shi = parseInt((i % 100) / 10);
            var bai = parseInt((i % 1000) / 100);
            var qian = parseInt((i % 10000) / 1000);
            if(ge == 2) {
                sum++;
            }
            if(shi == 2) {
                sum++;
            }
            if(bai == 2) {
                sum++;
            }
        }
        console.log('1~500之间2的出现次数是：' + sum);
    </script>
</body>
</html>